<template>
  <div class="bgc">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>黑马面经运营后台</span>
      </div>
      <el-form ref="loginrule" :model="form" :rules="rules">
        <el-form-item prop="username" label="用户名:">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码:">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="login">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'ElementPcIndex',

  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 15, message: '请输入6 ~ 15 位之间的用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,11}$/, message: '请输入5 ~ 11 位之间的用户名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.loginrule.validate(async valid => {
        if (!valid) return
        // console.log(1)
        const res = await login(this.form)
        console.log(res)
        this.$store.commit('user/setToken', res.data.token)
        this.$message.success('恭喜你，登陆成功')
        this.$router.push('/')
      })
    },
    reset () {
      this.$refs.loginrule.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.bgc{
  height: 100vh;
  background: url('../../assets/login-bg.svg') no-repeat center / cover;
}
.el-card {
  width: 420px;
  margin: 0 auto;
  transform: translateY(50%);
  .login{
    text-align: center;
  }
  ::v-deep .el-card__header{
    background-color: #727cf5;
    color: #fff;
    text-align: center;
  }
}
</style>
